package asso.onetomore;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBPoolUtils;

public class MumDaoImpl implements MumDao{

	@Override
	public Mum selectById(int id) {
		try {
			Connection c = DBPoolUtils.getConnection();
			String sql = "select mid,mname,childid,cname from mum left join children using(mid) where mid=?";
			ResultSet set = DBPoolUtils.query(c, sql, id);
			
			if(set.next()) {
				Mum m = new Mum();
				m.setmId(set.getInt(1));
				m.setmName(set.getString(2));
				
				List<Children> children = new ArrayList<>();
				do{
					Children ch = new Children();
					ch.setChildId(set.getInt(3));
					ch.setcName(set.getString(4));
					
					children.add(ch);
				}while(set.next());
				m.setChildren(children);
				return m;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
